<template>
  <div>
    <el-dialog
      :close-on-click-modal="false"
      :close-on-press-escape="false"
      :title="
        type == 1 ? '添加审核人' : type == 2 ? '编辑审核人' : '查看审核人'
      "
      :visible="open"
      width="500px"
      append-to-body
      @close="cancel()"
    >
      <el-form
        v-loading="loading"
        ref="form"
        :model="form"
        :rules="type == 1 || type == 2 ? rules : {}"
        label-width="100px"
      >
        <!-- 知识类目 下拉框 -->
        <el-form-item label="知识类目" prop="categoryId">
          <el-select
            v-if="(type == 1 || type == 2) && !categoryid"
            v-model="form.categoryId"
            placeholder="请选择知识类目"
            filterable
            clearable
          >
            <el-option
              v-for="item in categorylist"
              :label="item.categoryName"
              :key="item.categoryId"
              :value="item.categoryId"
            >
              {{ item.categoryName }}
            </el-option>
          </el-select>
          <div v-else>
            <div
              v-for="item in categorylist"
              :key="item.categoryId"
              v-if="item.categoryId == form.categoryId"
            >
              {{ item.categoryName }}
            </div>
          </div>
        </el-form-item>
        <!-- 类目审核人 -->
        <el-form-item label="类目审核人" prop="professionalId">
          <el-select
            v-if="type == 1 || type == 2"
            v-model="form.professionalId"
            placeholder="请选择类目审核人"
            filterable
            clearable
          >
            <el-option
              v-for="item in userList"
              :label="item.name"
              :key="item.userId"
              :value="parseInt(item.userId)"
            >
              {{ item.name }}
            </el-option>
          </el-select>
          <div v-else>
            <div
              v-for="item in userList"
              :key="item.userId"
              v-if="item.userId == form.professionalId"
            >
              {{ item.name }}
            </div>
          </div>
        </el-form-item>
      </el-form>
      <div
        slot="footer"
        class="dialog-footer"
        style="display: flex; justify-content: flex-end"
      >
        <el-button
          v-preventReClick
          v-if="type == 1 || type == 2"
          @click="cancel"
          >取 消</el-button
        >
        <el-button
          v-preventReClick
          type="primary"
          @click="submitForm"
          v-if="type == 1 || type == 2"
          >确 定</el-button
        >
        <el-button v-preventReClick @click="cancel" v-if="type == 3"
          >关 闭</el-button
        >
      </div>
    </el-dialog>
  </div>
</template>
    
  <script>
import {
  getCategoryList,
  putCategory,
  getCategory,
} from "@/api/knowledge/category";
import { mainAndManagerlist } from "@/api/schoolOrganization/student";
export default {
  props: {
    open: Boolean,
    type: [Number, String],
    categoryid: [Number, String],
  },
  data() {
    return {
      loading: false,
      userList: [],
      categorylist: [],
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        categoryId: [
          { required: true, message: "知识类目不能为空", trigger: "blur" },
        ],
        professionalId: [
          { required: true, message: "类目审核人不能为空", trigger: "blur" },
        ],
      },
    };
  },
  watch: {
    async open(e) {
      if (e) {
        await this.getCategorylist();
        await this.getUser();
        this.reset();
        this.loading = true;
        getCategory(this.categoryid)
          .then((res) => {
            this.form = res.data;
          })
          .finally(() => {
            this.loading = false;
          });
      }
    },
  },
  mounted() {},
  methods: {
    /* 获取总部的所有员工 */
    async getUser() {
      try {
        let res = await mainAndManagerlist();
        if (res.code == 200) {
          this.userList = res.data;
        }
      } finally {
      }
    },
    /* 获取分类列表*/
    async getCategorylist() {
      try {
        let res = await getCategoryList();
        if (res.code == 200) {
          this.categorylist = res.rows;
        }
      } finally {
      }
    },
    /** 提交按钮 */
    submitForm: function () {
      this.$refs["form"].validate(async (valid) => {
        if (valid) {
          try {
            let res = await putCategory(this.form);
            if (res.code == 200) {
              this.$message({
                message: "添加成功",
                type: "success",
              });
              this.$emit("update:open", false);
            }
          } finally {
          }
        }
      });
    },
    // 表单重置
    reset() {
      this.form = {
        categoryId: undefined,
        professionalId: undefined,
      };
      this.resetForm("form");
    },
    cancel() {
      this.$emit("update:open", false);
    },
  },
};
</script>
    <style scoped lang="scss"></style>
    